function  [OIm, v] = simplePreprocess( Im, path, elipseCache, CIR, center, rotate)
%SIMPLEPREPROCESS  Vybere z fotky jen oblicej.
% Im - vstupni obrazek, 
% path - jmeno obrazku (cesta)
% elipseCache - jestli se ma elipsa nacitat z cache
% CIR - [0,1] parametr kolik procent elipsy se ma brat
% center - jestli se ma vysledna elipsa presunout na stred
% rotate - jestli se ma elipsa otocit
% OIm - zpracovany obrazek
% v - popis elipsy ve tvaru [r1 r2 cx cy theta]

%defaultni hodnoty argumentu
if (nargin < 3)
    elipseCache = false;
end;
if (nargin < 4)
    CIR = 1.0;
end;
if (nargin < 5)
    center = true;
end;
if (nargin < 6)
    rotate = false;
end;

if (rotate && ~center)
    error('Otoceni je mozne provest, jen kdyz je soucasne obrazek vycentrovan.');
end

Imm = im2double(Im);

if elipseCache
    if ~exist('elipse_cache','dir')
        mkdir('elipse_cache');
    end
    path = strrep(path, '\', '$');
    path = strrep(path, '/', '$');
    path = ['elipse_cache/e_' path '.mat'];
    if exist(path, 'file')
        load(path, 'e');
    else
        Img2 = lightBalance(Imm);
        M = findFace2(Img2); %M .. mask 0 or 1 jestli uvnitr nebo vne
        e = findElipse2(M);
        save(path, 'e');
    end    
else
    Img2 = lightBalance(Imm);
    M = findFace2(Img2); %M .. mask 0 or 1 jestli uvnitr nebo vne
    e = findElipse2(M);
end


if CIR < 1
    e = reduceElipse(e, CIR);
end
[SizeY, SizeX] = size(Im);
Me = elipseMask(SizeX, SizeY, e);
OIm = Imm;
OIm(~Me) = 0;
v = solveelipse(e);
if center
    shX = round(SizeX/2 - v(3));
    shY = round(SizeY/2 - v(4));
    OIm = shiftImg(OIm, shX, shY);
    v(3) = v(3) + shX;
    v(4) = v(4) + shY;
    if rotate
        q = 10*(1 - min(v([1 2])) / max(v([1 2])))^2;
        if(q > 1)
            q = 1;
        end
        ra = q*v(5);
        OIm = imrotate(OIm, ra*180/pi, 'bilinear', 'crop');
        v(5) = v(5) - ra;
    end
end
